﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class HotelController : Controller
    {

        //注入
        private IHotelService _hotelService;
        public HotelController(IHotelService hotelService)
        {
            _hotelService = hotelService;
        }
        // GET: Hotel

        /// <summary>
        /// 多表查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult QueryHotel(int page, int limit)
        {
            ResponseResult resul = _hotelService.GetHotelInfo(page, limit);
            return Json(resul, JsonRequestBehavior.AllowGet);
        }

        //多表添加
        public JsonResult AddHotel(HotelTemporary temporar)
        {
            ResponseResult result = new ResponseResult();
            result.data = _hotelService.BatchAdd(temporar);
            return Json(result);
        }

        //多表编辑
        public JsonResult UpdateHotel(HotelTemporary temporary)
        {
            ResponseResult result = new ResponseResult();
            result.data = _hotelService.UpdateHotel(temporary);
            return Json(result);
        }

        //多表删除
        public JsonResult DeleteHotel(int id)
        {
            ResponseResult resul = new ResponseResult();
            resul.data = _hotelService.DeleteHotel(id);
            return Json(resul);
        }

        //上传图片
        public ActionResult Upload()
        {
            try
            {
                HttpFileCollectionBase files = Request.Files;
                HttpPostedFileBase file = files[0];
                //获取文件名后缀
                string extName = Path.GetExtension(file.FileName).ToLower();
                //获取保存目录的物理路径
                if (System.IO.Directory.Exists(Server.MapPath("/Images/")) == false)//如果不存在就创建images文件夹
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("/Images/"));
                }
                string path = Server.MapPath("/Images/"); //path为某个文件夹的绝对路径，不要直接保存到数据库
                                                          //    string path = "F:\\TgeoSmart\\Image\\";
                                                          //生成新文件的名称，guid保证某一时刻内图片名唯一（文件不会被覆盖）
                string fileNewName = Guid.NewGuid().ToString();
                string ImageUrl = path + fileNewName + extName;
                //SaveAs将文件保存到指定文件夹中
                file.SaveAs(ImageUrl);
                //此路径为相对路径，只有把相对路径保存到数据库中图片才能正确显示（不加~为相对路径）
                string url = "\\Images\\" + fileNewName + extName;
                return Json(new
                {
                    Result = true,
                    Data = url
                });
            }
            catch (Exception exception)
            {
                return Json(new
                {
                    Result = false,
                    exception.Message
                });
            }
        }

        //上传图片
        public ActionResult Uploads()
        {
            try
            {
                HttpFileCollectionBase files = Request.Files;
                HttpPostedFileBase file = files[0];
                //获取文件名后缀
                string extName = Path.GetExtension(file.FileName).ToLower();
                //获取保存目录的物理路径
                if (System.IO.Directory.Exists(Server.MapPath("/Images/")) == false)//如果不存在就创建images文件夹
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("/Images/"));
                }
                string path = Server.MapPath("/Images/"); //path为某个文件夹的绝对路径，不要直接保存到数据库
                                                          //    string path = "F:\\TgeoSmart\\Image\\";
                                                          //生成新文件的名称，guid保证某一时刻内图片名唯一（文件不会被覆盖）
                string fileNewName = Guid.NewGuid().ToString();
                string ImageUrl = path + fileNewName + extName;
                //SaveAs将文件保存到指定文件夹中
                file.SaveAs(ImageUrl);
                //此路径为相对路径，只有把相对路径保存到数据库中图片才能正确显示（不加~为相对路径）
                string url = "\\Images\\" + fileNewName + extName;
                return Json(new
                {
                    Result = true,
                    Data = url
                });
            }
            catch (Exception exception)
            {
                return Json(new
                {
                    Result = false,
                    exception.Message
                });
            }
        }
    }
}